package com.can.Service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.can.Mapper.GoodBadMapper;
import com.can.Mapper.PostMapper;
import com.can.Service.GoodBadService;
import com.can.domain.po.Goodbad;
import com.can.domain.po.post;
import com.can.domain.vo.postcommonvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodBadServiceImpl extends ServiceImpl<GoodBadMapper, Goodbad> implements GoodBadService {
    @Autowired
    GoodBadMapper goodBadMapper;
    @Autowired
    PostMapper postMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public postcommonvo getAllGoodBad() {

        try {
            List<post> posts = postMapper.selectList(null);
            ArrayList<Goodbad> res = new ArrayList<>();
            for (post post : posts) {
                int good=Integer.parseInt(redisTemplate.opsForValue().get("post:" + post.getId() + ":good"));
                int bad=Integer.parseInt(redisTemplate.opsForValue().get("post:" + post.getId() + ":bad"));
                res.add(new Goodbad(post.getId(),good,bad));

            }
            System.out.println(res);
            return new postcommonvo<>(200,"查询成功",res);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo<>(400,"查询失败",null);
        }

    }

    @Override
    public postcommonvo goodById(int id) {
        try {
            redisTemplate.opsForValue().increment("post:" + id + ":good");
            int good=Integer.parseInt(redisTemplate.opsForValue().get("post:" + id + ":good"));
            int bad=Integer.parseInt(redisTemplate.opsForValue().get("post:" + id + ":bad"));
            Goodbad res=new Goodbad(id,good,bad);
            System.out.println(res);
            return new postcommonvo<>(200,"操作成功",res);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo<>(400,"操作失败",null);
        }
    }
    @Override
    public postcommonvo badById(int id) {
        try {
            redisTemplate.opsForValue().increment("post:" + id + ":bad");
            int good=Integer.parseInt(redisTemplate.opsForValue().get("post:" + id + ":good"));
            int bad=Integer.parseInt(redisTemplate.opsForValue().get("post:" + id + ":bad"));
            Goodbad res=new Goodbad(id,good,bad);
            System.out.println(res);
            return new postcommonvo<>(200,"操作成功",res);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo<>(400,"操作失败",null);
        }
    }
//    @Override
//    public postcommonvo getAllGoodBad() {
//        try {
//            List<Goodbad> res = goodBadMapper.selectList(null);
//            return new postcommonvo<>(200,"查询成功",res);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new postcommonvo<>(400,"查询失败",null);
//        }
//
//    }
}
